package com.mzm.ui.admin;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JComboBox;

import org.jdesktop.swingx.combobox.ListComboBoxModel;

public class EnumComboBox extends JComboBox {

	private Enum[] enumConstants;

	public void setData(Class<? extends Enum> enumClass) {
		enumConstants = enumClass.getEnumConstants();
		List<String> l = new ArrayList<>();
		for (Enum e : enumConstants) {
			l.add(e.name());
		}
		this.setModel(new ListComboBoxModel<>(l));
	}

	public <T extends Enum> T getSelectedValue() {
		String s = (String) this.getSelectedItem();
		for (Enum e : enumConstants) {
			if(s.equals(e.name())){
				return (T) e;
			}
		}
		return null;
	}
}
